home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / access / common / heapvalid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.6 KB  |  147 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *    heapvalid.c
  4.  *    
  5.  *   DESCRIPTION
  6.  *    heap tuple qualification validity checking code
  7.  *
  8.  *   INTERFACE ROUTINES
  9.  *    heap_satisifies
  10.  *    heap_keytest
  11.  *    TupleUpdatedByCurXactAndCmd
  12.  *
  13.  *   OLD INTERFACE ROUTINES (turned into macros in valid.h -cim 4/30/91)
  14.  *    amvalidtup
  15.  *    ItemIdHasValidHeapTupleForQualification
  16.  *    keytest_tupdesc
  17.  *    keytest
  18.  *    
  19.  *   NOTES
  20.  *    
  21.  *   IDENTIFICATION
  22.  *    $Header: /private/postgres/src/access/common/RCS/heapvalid.c,v 1.13 1992/08/21 16:26:56 mao Exp $
  23.  * ----------------------------------------------------------------
  24.  */
  25.  
  26. #include "tmp/c.h"
  27.  
  28. #include "access/htup.h"
  29. #include "access/skey.h"
  30. #include "access/tqual.h"
  31. #include "access/valid.h"
  32. #include "access/xact.h"
  33.  
  34. #include "storage/buf.h"
  35. #include "storage/bufmgr.h"
  36. #include "storage/bufpage.h"
  37. #include "storage/itemid.h"
  38. #include "storage/page.h"
  39. #include "utils/fmgr.h"
  40. #include "utils/log.h"
  41. #include "utils/rel.h"
  42.  
  43. RcsId("$Header: /private/postgres/src/access/common/RCS/heapvalid.c,v 1.13 1992/08/21 16:26:56 mao Exp $");
  44.  
  45. /* ----------------
  46.  *    heap_keytest
  47.  *
  48.  *    Test a heap tuple with respect to a scan key.
  49.  * ----------------
  50.  */
  51. bool
  52. heap_keytest(t, tupdesc, nkeys, keys)
  53.     HeapTuple        t;
  54.     TupleDescriptor     tupdesc;
  55.     int            nkeys;
  56.     struct skey     keys[];
  57. {
  58.     Boolean    isnull;
  59.     DATUM    atp;
  60.     int        test;
  61.  
  62.     for (; nkeys--; keys++) {
  63.     atp = heap_getattr(t, InvalidBuffer,
  64.                keys->sk_attnum,  tupdesc, &isnull);
  65.     
  66.     if (isnull)
  67.         /* XXX eventually should check if SK_ISNULL */
  68.         return false;
  69.  
  70.     if (keys->sk_flags & SK_COMMUTE)
  71.         test = (int) (*(keys->func)) (keys->sk_data, atp);
  72.     else
  73.         test = (int) (*(keys->func)) (atp, keys->sk_data);
  74.     
  75.     if (!test == !(keys->sk_flags & SK_NEGATE))
  76.         return false;
  77.     }
  78.     
  79.     return true;
  80. }
  81.  
  82. /* ----------------
  83.  *    heap_tuple_satisfies
  84.  *
  85.  *  Returns a valid HeapTuple if it satisfies the timequal and keytest.
  86.  *  Returns NULL otherwise.  Used to be heap_satisifies (sic) which
  87.  *  returned a boolean.  It now returns a tuple so that we can avoid doing two
  88.  *  PageGetItem's per tuple.
  89.  *
  90.  *    Complete check of validity including LP_CTUP and keytest.
  91.  *    This should perhaps be combined with valid somehow in the
  92.  *    future.  (Also, additional rule tests/time range tests.)
  93.  *
  94.  *  on 8/21/92 mao says:  i rearranged the tests here to do keytest before
  95.  *  SatisfiesTimeQual.  profiling indicated that even for vacuumed relations,
  96.  *  time qual checking was more expensive than key testing.  time qual is
  97.  *  least likely to fail, too.  we should really add the time qual test to
  98.  *  the restriction and optimize it in the normal way.  this has interactions
  99.  *  with joey's expensive function work.
  100.  * ----------------
  101.  */
  102.  
  103. HeapTuple
  104. heap_tuple_satisfies(itemId, relation, disk_page, qual, nKeys, key)
  105.     ItemId    itemId;
  106.     Relation relation;
  107.     PageHeader disk_page;
  108.     TimeQual    qual;
  109.     ScanKeySize    nKeys;
  110.     ScanKey key;
  111. {
  112.     HeapTuple    tuple;
  113.     bool res;
  114.  
  115.     if (! ItemIdIsUsed(itemId) || ItemIdIsLock(itemId))
  116.     return NULL;
  117.  
  118.     tuple = (HeapTuple) PageGetItem((Page) disk_page, itemId);
  119.  
  120.     if (key != NULL)
  121.     res = keytest(tuple, relation, nKeys, (struct skey *) key);
  122.     else
  123.     res = TRUE;
  124.  
  125.     if (res && (relation->rd_rel->relkind == 'u'
  126.         || HeapTupleSatisfiesTimeQual(tuple,qual)))
  127.     return tuple;
  128.  
  129.     return (HeapTuple) NULL;
  130. }
  131.  
  132. /* ----------------
  133.  *    TupleUpdatedByCurXactAndCmd
  134.  * ----------------
  135.  */
  136. bool
  137. TupleUpdatedByCurXactAndCmd(t)
  138.     HeapTuple    t;
  139. {
  140.     if (TransactionIdEquals((TransactionId) t->t_xmax,
  141.                             GetCurrentTransactionId()) &&
  142.     t->t_cmax == (CID) GetCurrentCommandId())
  143.     return true;
  144.  
  145.     return false;
  146. }
  147.